package nbn.android.myyoutubeclient.parse;

import nbn.android.myyoutubeclient.object.PlaylistObject;

import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

public class ParserPlaylist {
	
	static String tag="ParserPlaylist";
	
	/** --------------------- Playlist Search TAG --------------------- */
	private static final String TAG_GROUP = "media:group";
//	private static final String TAG_AUTHOR = "author";
	private static final String TAG_TITLE = "title";
	private static final String TAG_DESCRIPTION = "yt:description";
	private static final String TAG_MEDIA_THUMB = "media:thumbnail";
	private static final String TAG_PLAYLIST_ID = "yt:playlistId";
	private static final String TAG_PLAYLIST_VIDEO = "gd:feedLink";

	/** ---------------------Playlist Attribute tags--------------------- */
	private static final String ATR_THUMB_NAME = "yt:name";
	private static final String ATR_NUMBER_VIDEO = "countHint";
	private static final String VALUE_ATR_POSTER = "mqdefault";
	private static final String VALUE_ATR_DEFAULT = "default";
	
	public static PlaylistObject getResult(NodeList searchresult, int position) {
		PlaylistObject result = new PlaylistObject();
		try {
			Element e = (Element) searchresult.item(position);
			String title= Parser.getValue(e, TAG_TITLE);
			String description= Parser.getValue(e, TAG_DESCRIPTION);
			String playlist_id= Parser.getValue(e, TAG_PLAYLIST_ID);
			String number=Parser.getAttributeValue(e, TAG_PLAYLIST_VIDEO, ATR_NUMBER_VIDEO);
			
			NodeList list=e.getElementsByTagName(TAG_GROUP);
			Element e1=(Element)list.item(0);
			String image_url=Parser.getImageUrlAttributeValue(TAG_MEDIA_THUMB,ATR_THUMB_NAME,VALUE_ATR_POSTER, e1);
			if(image_url==null){
				image_url = Parser.getImageUrlAttributeValue(TAG_MEDIA_THUMB, ATR_THUMB_NAME,VALUE_ATR_DEFAULT, e1);
			}
			result.settitle(title);
			result.setid(playlist_id);
			result.setdescription(description);
			result.setthumbnail(image_url);
			result.setnumber(number);
			
//			Log.i(tag,"title"+title);
//			Log.i(tag,"description"+description);
//			Log.i(tag,"id"+playlist_id);
//			Log.i(tag,"thumbnail"+image_url);
//			Log.i(tag,"number"+number);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}
}
